package com.fotkidemo.network.json;

import com.fotkidemo.beans.Album;
import com.fotkidemo.beans.EntryElement;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: roma
 * Date: 6/18/12
 * Time: 1:46 AM
 * To change this template use File | Settings | File Templates.
 */
public class JsonAlbumsParser extends AbstractJsonEntryParser {
    private static final String TAG = "JsonAlbumsParser";

    public JsonAlbumsParser(String json) {
        super(json);
    }

    @Override
    protected EntryElement parseEntry(JSONObject entry, String id, String author, String title, Date date) throws JSONException {
        Album album = new Album(id, author, title, date);

        JSONObject links = entry.getJSONObject("links");
        String photoURL = links.getString("photos");
        album.setPhotosURL(photoURL);
        int imageCount = entry.getInt("imageCount");
        album.setImageCount(imageCount);

        if (entry.has("img")) {
            JSONObject img = entry.getJSONObject("img");

            JSONObject xxs = img.getJSONObject("XXS");
            String hrefXXS = xxs.getString("href");
            album.setHrefXXS(hrefXXS);
            int heightXXS = xxs.getInt("height");
            album.setHeightXXS(heightXXS);
            int widthXXS = xxs.getInt("width");
            album.setWidthXXS(widthXXS);
        }

        return album;
    }

    @Override
    protected String getTag() {
        return TAG;
    }
}
